package de.unimannheim.ki.tests;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import weka.associations.Apriori;
import weka.associations.FPGrowth;

import weka.core.Instances;
import weka.core.SelectedTag;
import weka.core.converters.ConverterUtils.DataSource;
import weka.experiment.InstanceQuery;
import weka.gui.SelectedTagEditor;

public class Wekatest3 {
	
	public static void main(String args[]) {
		 BufferedReader reader;
		 Instances data = null;
		 InstanceQuery query = null;
		 try {
			query = new InstanceQuery();
			 query.setUsername("root");
			 query.setPassword("initial1");
			 query.setQuery("select * from testdata");
			 
			data = query.retrieveInstances();
			 
			 
			System.out.println( data.numInstances());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*
		try {
			 DataSource source = new DataSource("sample5.csv");
			 data = source.getDataSet();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		*/
	// setting class attribute
		
		 FPGrowth test = new FPGrowth();
		
	
		
		  Apriori apriori = new Apriori();
		  String options[] = new String[8];
		  options[0] = "2";
		  options[1] = "-1";
		  options[2] = "100";
		  options[3] = "0";
		  options[4] = "0.9";
		  options[5] = "0.01";
		  options[6] = "1.0";
		  options[7] = "0.03";
								
		
		  test.setDelta(0.01);
	      test.setLowerBoundMinSupport(0.02);
		  test.setFindAllRulesForSupportLevel(true);
		  try {
			test.buildAssociations(data);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println(test.getAssociationRules().getNumRules());
		  /*
		  apriori.setCar(false);
		  apriori.setClassIndex(-1);
		  apriori.setDelta(0.5);
		  apriori.setLowerBoundMinSupport(0.01);
	
		
		  apriori.setNumRules(100);
		  apriori.setSignificanceLevel(-1.0);
		  apriori.setUpperBoundMinSupport(1.0);
		  
		  */
		    try {
		    
				//apriori.buildAssociations(data);
//				test.buildAssociations(data);
//				boolean isinPremise = false;
//		    	String totestitem = "674";
//				System.out.println(test.toString());
//				
//				List<AssociationRule> rules = test.getAssociationRules();
//				List<String> itemsrelated = new ArrayList<String>();
//				for (AssociationRule rule : rules) {
//					Collection<BinaryItem> tester = rule.getPremise();
//					for (BinaryItem item : tester) {
//						if(item.getAttribute().name().equals(totestitem)){
//							 isinPremise = true; 
//							 break;
//						}
//					}
//					if(isinPremise){
//						tester = rule.getConsequence();
//						for (BinaryItem item : tester) {
//							if( !itemsrelated.contains(item.getAttribute().name()))
//							itemsrelated.add(item.getAttribute().name());
//						}
//						 isinPremise = false;
//					}
//				}//items related get
//				
//				System.out.println("Itemrelated:");
//				for (String string : itemsrelated) {
//					System.out.println(string);
//				}
				
				//apriori.getAllTheRules();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
}
